## R code to extract micro support for democracy in US samples
#### Claassen & Magalhaes, 
#### Public Support for Democracy in the United States Has Declined Generationally
#### POQ
#### January 2023

library(haven)
library(ltm)

# options
options(mc.cores = parallel::detectCores())
par(mfrow=c(1,1), mar=c(3,3,1,1), tcl=-0.2, las=0, mgp=c(1.5, 0.4, 0))

# folders
WD = rstudioapi::getActiveDocumentContext()$path 
setwd(dirname(WD))
print( getwd() )

## Read data

# WVS
# This data is not included here; 
# It is a dataset contructed from the WVS 1981-2017 trend file, with the United States samples extracted. 
# This trend file can be downloaded from the WVS website
wvs = read_dta("WVS_TimeSeries_USA.dta")

# LAPOP / Americasbarometer
# These datasets are not included here;
# These US-specific datasets can be downloaded from the Americasbarometer website
lap06 = read_dta("LAPOP USA 2006.dta")
lap17 = read_dta("LAPOP USA 2017.dta")
lap19 = read_dta("LAPOP USA 2019.dta")
lap08 = read_spss("LAPOP USA 2008.sav")
lap10 = read_spss("LAPOP USA 2010.sav")
lap12 = read_spss("LAPOP USA 2012.sav")
lap14 = read_spss("LAPOP USA 2014.sav")

# state codes
state_codes = read.csv("state_codes.csv")



### LAPOP recodes


## Churchill question
# 2006: ING4 - 1-7 scale - high = sup dem
# 2008: ing4
# 2010: ing4
# 2012: ing4
# 2014: ing4
# 2017: ing4
# 2019: ing4

lap06$dem_best = as.vector(car::recode(lap06$ING4, "1=1; 2=2; 3=3; 4=4; 5=5; 6=6; 7=7; else=NA"))
lap08$dem_best = as.vector(car::recode(lap08$ing4, "1=1; 2=2; 3=3; 4=4; 5=5; 6=6; 7=7; else=NA"))
lap10$dem_best = as.vector(car::recode(lap10$ing4, "1=1; 2=2; 3=3; 4=4; 5=5; 6=6; 7=7; else=NA"))
lap12$dem_best = as.vector(car::recode(lap12$ing4, "1=1; 2=2; 3=3; 4=4; 5=5; 6=6; 7=7; else=NA"))
lap14$dem_best = as.vector(car::recode(lap14$ing4, "1=1; 2=2; 3=3; 4=4; 5=5; 6=6; 7=7; else=NA"))
lap17$dem_best = as.vector(car::recode(lap17$ing4, "1=1; 2=2; 3=3; 4=4; 5=5; 6=6; 7=7; else=NA"))
lap19$dem_best = as.vector(car::recode(lap19$ing4, "1=1; 2=2; 3=3; 4=4; 5=5; 6=6; 7=7; else=NA"))

## 3 statements
# 2006: DEM2 - 1-don't matter; 2=democ; 3=auth; 8=DK
# 2008: dem2; 8=DK
# 2010: dem2 - 1-don't matter; 2=democ; 3=auth; 88=DK
# 2012: dem2 - 1-don't matter; 2=democ; 3=auth; 88=DK
# 2014: dem2; 888888=DK
# 2017 - not asked
# 2019 - not asked

lap06$dem_pref = as.vector(car::recode(lap06$DEM2, "3=1; 1=2; 3=3; 8=2; else=NA"))
lap08$dem_pref = as.vector(car::recode(lap08$dem2, "3=1; 1=2; 3=3; 8=2; else=NA"))
lap10$dem_pref = NA
lap12$dem_pref = NA
lap14$dem_pref = NA
lap17$dem_pref = NA
lap19$dem_pref = NA

## age / birthyear
# 2006: Q2 - age in years
# 2008: birthyr
# 2010: q2 - age in years
# 2012: q2 
# 2014: q2 
# 2017: q2 
# 2019: q2 

lap06$age = lap06$Q2
lap08$age = 2008 - lap08$birthyr
lap10$age = lap10$q2
lap12$age = lap12$q2
lap14$age = lap14$q2
lap17$age = lap17$q2
lap19$age = lap19$q2

lap06$birthyr = 2006 - lap06$age 
lap10$birthyr = 2010 - lap10$age 
lap12$birthyr = 2012 - lap12$age
lap14$birthyr = 2014 - lap14$age
lap17$birthyr = 2017 - lap17$age
lap19$birthyr = 2019 - lap19$age

## gender
# 2006: Q1 - female=2
# 2008: q1 - female=2
# 2010: q1 - female=2
# 2012: q1 - female=2
# 2014: q1 - female=2
# 2017: q1 - female=2
# 2019: q1 - female=2

lap06$female = ifelse(lap06$Q1==2, 1, 0)
lap08$female = ifelse(lap08$q1==2, 1, 0)
lap10$female = ifelse(lap10$q1==2, 1, 0)
lap12$female = ifelse(lap12$q1==2, 1, 0)
lap14$female = ifelse(lap14$q1==2, 1, 0)
lap17$female = ifelse(lap17$q1==2, 1, 0)
lap19$female = ifelse(lap19$q1==2, 1, 0)

## education
# 2006: ED - degree = 13-16
# 2008: educ - 6-pt scale; degree = 5+6
# 2010: educ - 6-pt scale; degree = 5+6
# 2012: educ - 6-pt scale; degree = 5+6
# 2014: used - 6-pt scale; degree = 5+6
# 2017: ed_usa - 6-pt scale; degree = 5+6
# 2019: ed_usa - 6-pt scale; degree = 5+6

lap06$degree = ifelse(lap06$ED > 12, 1, 0)
lap08$degree = ifelse(lap08$educ > 4, 1, 0)
lap10$degree = ifelse(lap10$educ > 4, 1, 0)
lap12$degree = ifelse(lap12$educ > 4, 1, 0)
lap14$degree = ifelse(lap14$used > 4, 1, 0)
lap17$degree = ifelse(lap17$ed_usa > 4, 1, 0)
lap19$degree = ifelse(lap19$ed_usa > 4, 1, 0)

## income 
# 2006: Q10 - 1-5 scale (annual income)
# 2008: income - 1-14 scale (annual income); 15=NA
# 2010: q10 - 0-10 scale, 0=0
# 2012: q10 - 1-17 scale, 1=0
# 2014: q10new - 1-16 scale, 17=0
# 2017: q10new - 1-16 scale, 17=0
# 2019: q10new - 1-16 scale

lap06$income_std = scale(lap06$Q10)[,1]
lap08$income_std = scale(car::recode(lap08$income, "15=NA"))[,1]
lap10$income_std = scale(lap10$q10)[,1]
lap12$income_std = scale(lap12$q10)[,1]
lap14$income_std = scale(car::recode(lap14$q10new, "17=0"))[,1]
lap17$income_std = scale(car::recode(lap17$q10new, "17=0"))[,1]
lap19$income_std = scale(lap19$q10new)[,1]

## ethnicity & race
# 2006: q43_6 - white=1
# 2008: race - white = 1
# 2010: race - white = 1
# 2012: etid - white = 1
# 2014: race - white = 1
# 2017: etid - white = 1
# 2019: etid - white = 1

lap06$white = ifelse(lap06$q43_6==1, 1, 0)
lap08$white = ifelse(lap08$race==1, 1, 0)
lap10$white = ifelse(lap10$race==1, 1, 0)
lap12$white = ifelse(lap12$etid==1, 1, 0)
lap14$white = ifelse(lap14$race==1, 1, 0)
lap17$white = ifelse(lap17$etid==1, 1, 0)
lap19$white = ifelse(lap19$etid==1, 1, 0)

## state
# 2006: state (two-digit alpha code)
# 2008: inputstate (full state name)
# 2010: inputstate (full state name)
# 2012: inputstate (full state name)
# 2014: prov (full state name)
# 2017: prov (full state name)
# 2019: prov (full state name)

names(state_codes) = c("state_name", "FIPS_code", "USPS_code")

lap06$USPS_code = lap06$state
lap06 = merge(lap06, state_codes, by="USPS_code", all.x=TRUE)

lap08$FIPS_code = lap08$inputstate
lap08 = merge(lap08, state_codes, by="FIPS_code", all.x=TRUE)

lap10$FIPS_code = lap10$inputstate
lap10 = merge(lap10, state_codes, by="FIPS_code", all.x=TRUE)

lap12$FIPS_code = lap10$inputstate
lap12 = merge(lap12, state_codes, by="FIPS_code", all.x=TRUE)

lap14$FIPS_code = lap14$prov - 4000
lap14 = merge(lap14, state_codes, by="FIPS_code", all.x=TRUE)

lap17$FIPS_code = lap17$prov - 4000
lap17 = merge(lap17, state_codes, by="FIPS_code", all.x=TRUE)

lap19$FIPS_code = lap19$prov - 4000
lap19 = merge(lap19, state_codes, by="FIPS_code", all.x=TRUE)

## PID
# 2006: VB11 - 1=Dem; 2=Rep; 3=Ind; 4=other; 5=not sure
# 2008: pid3 - 1=Dem; 2=Rep; 3=Ind; 4=other; 5=not sure 
# 2010: pid3 - 1=Dem; 2=Rep; 3=Ind; 4=other; 5=not sure
# 2012: pid3 - 1=Dem; 2=Rep; 3=Ind; 4=other; 5=not sure
# 2014: usvb1011 - 4001=Rep; 4002=Dem; 4003=Ind; 77=other
# 2017: usvb1011 - 4001=Rep; 4002=Dem; 4003=Ind; 77=other
# 2019: usvb1011 - 4001=Rep; 4002=Dem; 4003=Ind; 77=other

lap06$democrat = ifelse(lap06$VB11==1, 1, 0)
lap06$republican = ifelse(lap06$VB11==2, 1, 0)

lap08$democrat = ifelse(lap08$pid3==1, 1, 0)
lap08$republican = ifelse(lap08$pid3==2, 1, 0)

lap10$democrat = ifelse(lap10$pid3==1, 1, 0)
lap10$republican = ifelse(lap10$pid3==2, 1, 0)

lap12$democrat = ifelse(lap12$pid3==1, 1, 0)
lap12$republican = ifelse(lap12$pid3==2, 1, 0)

lap14$democrat = ifelse(lap14$usvb1011==4002, 1, 0)
lap14$republican = ifelse(lap14$usvb1011==4001, 1, 0)

lap17$democrat = ifelse(lap17$usvb1011==4002, 1, 0)
lap17$republican = ifelse(lap17$usvb1011==4001, 1, 0)

lap19$democrat = ifelse(lap19$usvb1011==4002, 1, 0)
lap19$republican = ifelse(lap19$usvb1011==4001, 1, 0)

## ideology
# 2006: ?
# 2008: ideo5
# 2010: l1b - 1=liberal; 10-conservative
# 2012: l1b - 1=liberal; 10-conservative
# 2014: l1b - 1=liberal; 10-conservative
# 2017: l1b - 1=liberal; 10-conservative
# 2019: l1b - 1=liberal; 10-conservative

## weights
# 2006: NA
# 2008: wt
# 2010: wt
# 2012: wt
# 2014: wt
# 2017: wt
# 2019: wt

lap06$wt = 1

## year

lap06$year = 2006
lap08$year = 2008
lap10$year = 2010
lap12$year = 2012
lap14$year = 2014
lap17$year = 2017
lap19$year = 2019

## merge

lap.vars = c("dem_best", "dem_pref", "age", "female", "degree", "income_std", "white", "state_name", "FIPS_code", 
             "USPS_code", "democrat", "republican", "year", "birthyr", "wt")

lap06 = lap06[, lap.vars]
lap08 = lap08[, lap.vars]
lap10 = lap10[, lap.vars]
lap12 = lap12[, lap.vars]
lap14 = lap14[, lap.vars]
lap17 = lap17[, lap.vars]
lap19 = lap19[, lap.vars]

lap_us = dplyr::bind_rows(lap06, lap08, lap10, lap12, lap14, lap17, lap19)

## generations
# silent: born before 1946
# boomer: born between 1946 and 1964
# gen x: born between 1965 and 1979
# millenial: born between 1980 and 1994
# gen y: born from 1995

lap_us$generation = ifelse(lap_us$birthyr > 1994, 5, 
                      ifelse(lap_us$birthyr < 1946, 1,      
                        ifelse(lap_us$birthyr > 1945 & lap_us$birthyr < 1965, 2,   
                          ifelse(lap_us$birthyr > 1964 & lap_us$birthyr < 1980, 3, 4))))

lap_us$birthdec = ifelse(lap_us$birthyr < 1920, 1, 
                    ifelse(lap_us$birthyr < 1930, 2,
                      ifelse(lap_us$birthyr < 1940, 3,
                        ifelse(lap_us$birthyr < 1950, 4,
                          ifelse(lap_us$birthyr < 1960, 5,
                            ifelse(lap_us$birthyr < 1970, 6,
                              ifelse(lap_us$birthyr < 1980, 7,
                                ifelse(lap_us$birthyr < 1990, 8,
                                  ifelse(lap_us$birthyr < 2000, 9, 10)))))))))

lap_us$cohort_5yr = cut(lap_us$birthyr, breaks=seq(1910, 2000, by=5))

lap_us$agecat = ifelse(lap_us$age < 30, 1,
                  ifelse(lap_us$age < 40, 2,
                    ifelse(lap_us$age < 50, 3, 
                      ifelse(lap_us$age < 60, 4, 
                        ifelse(lap_us$age < 70, 5, 6)))))

lap_us$agecat3 = ifelse(lap_us$age < 30, 1,
                   ifelse(lap_us$age < 60, 2, 3))

lap_us$agecat3a = ifelse(lap_us$age < 35, 1,
                    ifelse(lap_us$age < 60, 2, 3))

lap_us$agecat4 = ifelse(lap_us$age < 30, 1,
                 ifelse(lap_us$age < 45, 2,
                 ifelse(lap_us$age < 60, 3, 4)))

## South

table(lap_us$USPS_code, useNA="ifany")
lap_us$south = ifelse(lap_us$USPS_code %in% 
                        c("AR", "AL", "DE", "FL", "GA", "KY", "LA", "MD", "MS", "NC", "OK", "SC", "TN", "TX", 
                          "VA", "WV"), 1, 0)


## Party

lap_us$partyid = ifelse(lap_us$democrat==1, "democrat", 
                   ifelse(lap_us$republican==1, "republican", "other"))

summary(lap_us)


write.csv(lap_us, "lapop_us_democ.csv", row.names=FALSE)




### WVS recodes


names(wvs)[1:1000]
names(wvs)[1001:1056]

wvs = wvs[wvs$S002 > 2, ]

# Evaluate democracy: E117 - 1-4 scale (reverse)

wvs$E117
table(wvs$S002, wvs$E117, useNA="ifany")
wvs$eval_dem = car::recode(wvs$E117, "1=4; 2=3; 3=2; 4=1; else=NA")

# Democracy best: E123 - 1-4 scale

wvs$E123
table(wvs$S002, wvs$E123, useNA="ifany")
wvs$dem_best = car::recode(wvs$E123, "1=4; 2=3; 3=2; 4=1; else=NA")

# Reject army rule: E116 - 1-4 scale

wvs$E116
table(wvs$S002, wvs$E116, useNA="ifany")
wvs$rej_milrule = wvs$E116

# Reject strong leader: E114 - 1-4 scale

wvs$E114
table(wvs$S002, wvs$E114, useNA="ifany")
wvs$rej_strlead = wvs$E114

# Reject technocrat: E115 - 1-4 scale

wvs$E115
table(wvs$S002, wvs$E115, useNA="ifany")
wvs$rej_expert = wvs$E115

psych::alpha(wvs[, c("eval_dem", "dem_best", "rej_milrule", "rej_strlead", "rej_expert")])
psych::alpha(wvs[, c("eval_dem", "rej_milrule", "rej_strlead", "rej_expert")])
psych::alpha(wvs[, c("rej_milrule", "rej_strlead", "rej_expert")])

wvs$rej_aut_add = scale(wvs$rej_milrule + wvs$rej_strlead + wvs$rej_expert)[,1]

wvs_rejaut_dat = wvs[, c("rej_milrule", "rej_strlead", "rej_expert")]
wvs_rejaut_mod = grm(wvs_rejaut_dat, Hessian=TRUE)
summary(wvs_rejaut_mod)
wvs_rejaut_out = factor.scores(wvs_rejaut_mod, method="EAP")[[1]][,c(1:3, 6)]
names(wvs_rejaut_out)[4] = "rej_aut"
wvs = merge(wvs, wvs_rejaut_out, by = c("rej_milrule", "rej_strlead", "rej_expert"))
wvs$rej_aut = scale(wvs$rej_aut)[,1] * -1
summary(wvs$rej_aut)

wvs_supdem_dat = wvs[, c("eval_dem", "dem_best", "rej_milrule", "rej_strlead", "rej_expert")]
wvs_supdem_mod = grm(wvs_supdem_dat, Hessian=TRUE)
summary(wvs_supdem_mod)
wvs_supdem_out = factor.scores(wvs_supdem_mod, method="EAP")[[1]][,c(1:5, 8)]
names(wvs_supdem_out)[6] = "sup_dem"
wvs = merge(wvs, wvs_supdem_out, by = c("eval_dem", "dem_best", "rej_milrule", "rej_strlead", "rej_expert"))
wvs$sup_dem = scale(wvs$sup_dem)[,1] * -1
summary(wvs$sup_dem)

wvs_supdem_dat_2 = wvs[, c("eval_dem", "rej_milrule", "rej_strlead", "rej_expert")]
wvs_supdem_mod_2 = grm(wvs_supdem_dat_2, Hessian=TRUE)
summary(wvs_supdem_mod_2)
wvs_supdem_out_2 = factor.scores(wvs_supdem_mod_2, method="EAP")[[1]][,c(1:4, 7)]
names(wvs_supdem_out_2)[5] = "sup_dem_2"
wvs = merge(wvs, wvs_supdem_out_2, by = c("eval_dem", "rej_milrule", "rej_strlead", "rej_expert"))
wvs$sup_dem_2 = scale(wvs$sup_dem_2)[,1] 
summary(wvs$sup_dem_2)

cor(wvs[, c("sup_dem_2", "sup_dem", "rej_aut")])


# Year / wave: S020

table(wvs$S020)
wvs$year = wvs$S020

# Party ID

wvs$E179_WVS7 # Which party would you vote for 
table(wvs$E179_WVS7, useNA="ifany")
table(wvs$year, wvs$E179_WVS7, useNA="ifany")
wvs$democrat = ifelse(wvs$E179_WVS7==840002, 1, 0)
wvs$republican = ifelse(wvs$E179_WVS7==840001, 1, 0)

# Education

wvs$X025
wvs$X025A2
table(wvs$X025, useNA="ifany")
table(wvs$year, wvs$X025, useNA="ifany")
table(wvs$year, wvs$X025A2, useNA="ifany")
wvs$degree = ifelse(wvs$year==1995 & wvs$X025CSWVS==840123, 1,
               ifelse(wvs$year==1999 & wvs$X025CSWVS==840123, 1,
                 ifelse(wvs$year==2006 & wvs$X025CSWVS %in% c(840006, 840007, 840008, 840009), 1, 
                   ifelse(wvs$year==2011 & wvs$X025CSWVS %in% c(840112, 840113, 840114), 1, 
                      ifelse(wvs$year==2017 & wvs$X025CSWVS %in% c(840006, 840007, 840008, 840009), 1, 0)))))
table(wvs$year, wvs$degree, useNA="ifany")
table(wvs$year, wvs$degree, useNA="ifany") / rowSums(table(wvs$year, wvs$degree, useNA="ifany")) * 100

# Income

by(wvs$scaleinc, wvs$year, mean, na.rm=TRUE)
wvs[wvs$year==1995, "income_std"] = scale(wvs[wvs$year==1995, "scaleinc"])
wvs[wvs$year==1999, "income_std"] = scale(wvs[wvs$year==1999, "scaleinc"])
wvs[wvs$year==2006, "income_std"] = scale(wvs[wvs$year==2006, "scaleinc"])
wvs[wvs$year==2011, "income_std"] = scale(wvs[wvs$year==2011, "scaleinc"])
wvs[wvs$year==2017, "income_std"] = scale(wvs[wvs$year==2017, "scaleinc"])

# Gender

wvs$X001
table(wvs$X001, useNA="ifany")
table(wvs$year, wvs$X001, useNA="ifany")
wvs$female = ifelse(wvs$X001==2, 1, 0)

# Age, generation 

wvs$birthyr = wvs$yearbirth
summary(wvs$birthyr)
wvs$decbirth = ifelse(wvs$birthyr < 1910, 1, 
                  ifelse(wvs$birthyr < 1920, 2,
                    ifelse(wvs$birthyr < 1930, 3,
                      ifelse(wvs$birthyr < 1940, 4,
                        ifelse(wvs$birthyr < 1950, 5,
                          ifelse(wvs$birthyr < 1960, 6,
                            ifelse(wvs$birthyr < 1970, 7,
                              ifelse(wvs$birthyr < 1980, 8,
                                ifelse(wvs$birthyr < 1990, 9, 10)))))))))

wvs$cohort_5yr = cut(wvs$birthyr, breaks=seq(1900, 2000, by=5))

wvs$generation = ifelse(wvs$birthyr > 1994, 5, 
                    ifelse(wvs$birthyr < 1946, 1,      
                       ifelse(wvs$birthyr > 1945 & wvs$birthyr < 1965, 2,   
                          ifelse(wvs$birthyr > 1964 & wvs$birthyr < 1980, 3, 4))))

wvs$agecat = ifelse(wvs$age < 30, 1,
               ifelse(wvs$age < 40, 2,
                  ifelse(wvs$age < 50, 3, 
                     ifelse(wvs$age < 60, 4, 
                       ifelse(wvs$age < 70, 5, 6)))))

wvs$agecat3 = ifelse(wvs$age < 30, 1,
                ifelse(wvs$age < 60, 2, 3))

wvs$agecat3a = ifelse(wvs$age < 35, 1,
                 ifelse(wvs$age < 60, 2, 3))

wvs$agecat4 = ifelse(wvs$age < 30, 1,
                ifelse(wvs$age < 45, 2,
                  ifelse(wvs$age < 60, 3, 4)))

# Weight: S017

summary(wvs$S017)
wvs$wt = wvs$S017

# State

table(wvs$X048WVS, useNA = "ifany")
table(wvs$year, wvs$X048WVS, useNA = "ifany")
table(wvs$South, useNA = "ifany")
table(wvs$South, wvs$X048WVS, useNA = "ifany")

wvs$south = ifelse(is.na(wvs$X048WVS), NA, wvs$South)

tail(names(wvs), 30)
vars.wvs = c("eval_dem", "dem_best", "sup_dem", "sup_dem_2", "rej_milrule", "rej_strlead", "rej_expert", "rej_aut", 
             "year", "democrat", "republican", "south", "wt", "age", "agecat", "agecat4", "agecat3a", "birthyr", 
             "decbirth", "generation", "income_std", "white", "degree", "female")
wvs_us = as.data.frame(wvs[, vars.wvs])

write.csv(wvs_us, "wvs_us_democ.csv", row.names=FALSE)


